﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Text;
using IFPRJ.EF.Model;
using IFPRJ.EF.DALayer;

namespace IFPRJ.EF.DALayer.Providers.Sqlserver
{
    public class PostProvider:IPost
    {
        public static IFDBContext dbcontext = new IFDBContext();

        public PostProvider()
        { 
        }

        IFDBContext GetCurrentDBContext()
        {
            if (dbcontext == null)
                return new IFDBContext();
            else
                return dbcontext;
            
        }


        public bool PostCreate(Post post)
        {
            IFDBContext db=GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Posts.Add(post);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;
         
            }
            return result;
        }

        public bool PostUpdate(Post post)
        {

            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Entry<Post>(post).State = EntityState.Modified;
                db.SaveChanges(); 
                result = true;
               
                
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public bool PostDelete(Post post)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Posts.Remove(post);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
             
        }

        public List<Post> PostLoadAll()
        {
            IFDBContext db = GetCurrentDBContext();
            return  db.Posts.Take(10).ToList();
            
        }

        public Post PostGetSingleByPostId(int postid)
        {
            IFDBContext db = GetCurrentDBContext();
            Post post =db.Posts.Single(item => item.ID == postid);
            return post ?? null;
            
        }

        public List<Post> PostGetListByCatoryId(int catoryid)
        {
            IFDBContext db = GetCurrentDBContext();
            
            var postlist=   from p in db.Posts
                            where p.Post_CatoryID == catoryid
                            select p;
            return postlist.ToList();

        }
    }



    public class PostCatoryProvider : IPostCatory
    {
        public static IFDBContext dbcontext = new IFDBContext();

        public PostCatoryProvider()
        { 
        }

        IFDBContext GetCurrentDBContext()
        {
            if (dbcontext == null)
                return new IFDBContext();
            else
                return dbcontext;
            
        }
        public bool PostCatoryCreate(Post_Catory post_Catory)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Post_Catories.Add(post_Catory);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public bool PostCatoryUpdate(Post_Catory post_Catory)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Entry<Post_Catory>(post_Catory).State = EntityState.Modified;
                db.SaveChanges();
                result = true;


            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public bool PostCatoryDelete(Post_Catory post_Catory)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Post_Catories.Remove(post_Catory);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public List<Post_Catory> PostCatoryLoadAll()
        {
            IFDBContext db = GetCurrentDBContext();
            return db.Post_Catories.Take(10).ToList();
        }

        public Post_Catory PostCatoryGetSingleByCatoryId(int post_Catoryid)
        {
            IFDBContext db = GetCurrentDBContext();
            Post_Catory postcatory = db.Post_Catories.Single(item => item.ID == post_Catoryid);
            return postcatory ?? null;
        }
    }
}
